package method;

import lib.Paire;
import lib.Output;
import java.util.Iterator;
import java.util.TreeMap;
import lib.PrimeFactors;

public class CalculateMethod extends TreeMap<Integer, Output> {

    public int entiermax;

    public CalculateMethod(int entiermax) {
        this.entiermax = entiermax;
        notFactorOutPut();
//        factorOutPut();

    }

    private void notFactorOutPut() {

        int p;
        Output output;
        Paire paire;
        for (int i = 2 ; i <= entiermax ; i++) {
            for (int j = 2 ; j <= entiermax ; j++) {
                p = i * j;
                paire = new Paire(i, j);
                if (!containsKey(p)) {
                    put(p, new Output(paire, 1));
                } else {
                    output = get(p);
                    output.multip += 1;
                    output.add(paire);
                }
            }
        }
    }

    private void factorOutPut() {

        int p;
        Output output;
        Paire paire;
        for (int i = 2 ; i <= entiermax ; i++) {
            for (int j = i ; j <= entiermax ; j++) {
                p = i * j;
                paire = new Paire(i, j);
                if (!containsKey(p)) {
                    put(p, new Output(paire, new PrimeFactors(p), 1));
                } else {
                    output = get(p);
                    output.multip += 1;
                    output.add(paire);
                }
            }
        }
    }

    public String toText() {
        String retS = "";

        for (Iterator<Integer> it = keySet().iterator() ; it.hasNext() ;) {
            Output output = get(it.next());
//            if (output.multip != 1) {
            retS += output.toString();
//            }
        }

        return retS;
    }

    public Integer[][] toMatrixText() {

        Integer[][] tab = new Integer[ entiermax + 1 ][ entiermax + 1 ];
        for (Iterator<Integer> it = keySet().iterator() ; it.hasNext() ;) {
            Output output = get(it.next());
            for (Paire paire : output) {
                tab[paire.i][paire.j] = output.multip;
            }
        }

        return tab;
    }
}
